<?php  if ( ! defined('APPPATH')) exit('No direct script access allowed');
/**
 *      [PinTuXiu] (C)2001-2099 ONightjar.com Pintuxiu.com.
 *      This is NOT a freeware, use is subject to license terms
*/
class user_controller extends PZ_Controller{

	public function __construct() {
		parent::__construct();
		$this->authfunctions = array('addfollow','removefollow','updateuserinfo','resetpasswd','uploadavatar','saveavatar','nicknameupdatecheck','savetheme');
	}

	public function logout(){
		$userapi = load_api('user');
		$result = $userapi->logout();
		$this->redirect(site_url(config_item('default_module')),lang('200_logoutsuccess','tip').$result);
	}

	public function login(){
		$url = ($_SERVER['HTTP_REFERER']==null||$_SERVER['HTTP_REFERER']==host_url(site_url('user/login')))?site_url(config_item('default_module')):$_SERVER['HTTP_REFERER'];
		if($this->islogin){
			$this->jump($url);
			return;
		}

		$this->vcodesetting = load_api('setting')->getVcodeSetting();
		if($data['email'] = $this->param('email')){
			if(!$this->vcheck('login')){
				$this->ajax_failed_response(lang('500_vcode','tip'));
				return;
			}
			$data['password'] = $this->param('password');
			$data['remember'] = $this->param('remember');
			$userapi = load_api('user');
			$result = $userapi->login($data);
			if($result===501){
				$this->ajax_failed_response(lang('501_password_wrong','tip'));
			}elseif($result===502){
				$this->ajax_failed_response(lang('502_user_notexist','tip'));
			}else{
				$this->ajax_success_response(array('url'=>$url), lang('200_loginsuccess','tip'));
			}
			return;
		}
		$this->socialapis = load_api('setting')->getSetting('socialapi');
		$this->output("user/login");
		return;
	}

	public function register(){
		$url = ($this->method=='register'||$_SERVER['HTTP_REFERER']==null)?site_url(config_item('default_module')):$_SERVER['HTTP_REFERER'];
		if($this->islogin){
			$this->redirect($url, lang('200_loginsuccess','tip'));
			return;
		}

		$this->vcodesetting = load_api('setting')->getVcodeSetting();
		if($data['email'] = $this->param('email')){
			if(!$this->vcheck('register')){
				$this->ajax_failed_response(lang('500_vcode','tip'));
				return;
			}
			$data['nickname'] = $this->param('nickname');
			$password = $this->param('password');
			$data['is_active'] = 0;

			$fvalidation = load_class('Form_validation');

			$fvalidation->set_rules('email', 'lang:email:field' , 'trim|required|min_length[3]|valid_email|xss_clean|callback_cemailcheck');
			$fvalidation->set_rules('nickname', 'lang:nickname:field' , 'trim|required|min_length[2]|max_length[20]|xss_clean|callback_cnicknamecheck');
			$fvalidation->set_rules('password', 'lang:password:field' , 'trim|required|min_length[6]|max_length[40]|matches[passconf]');
			$fvalidation->set_rules('passconf', 'lang:repassword:field' , 'trim|required|min_length[6]|max_length[40]');

			if ($fvalidation->run() == FALSE){
				$this->ajax_failed_response($fvalidation->error_string());
				return FALSE;
			}else{
				$userdb = load_db('user');
				$data["passwd"] = md5($password);
				if($this->ucopen){
					$uc_password = $password;
					$uc_username = strtolower($data['nickname']);
					$uc_email = strtolower($data['email']);
					$ucenter = load_api('ucenter');
					$uc_id = $ucenter->uc_user_register($uc_username, $uc_password, $uc_email);
					if($uc_id <= 0) {
						if($uc_id == -1) {
							$message = lang('500_nickname_invalid','tip');
						} elseif($uc_id == -2) {
							$message =lang('500_contain_invalid_word','tip');
						} elseif($uc_id == -3) {
							$message =lang('500_nickname_existed','tip');
						} elseif($uc_id == -4) {
							$message =lang('500_email_not_valid','tip');
						} elseif($uc_id == -5) {
							$message =lang('500_email_notallowed','tip');
						} elseif($uc_id == -6) {
							$message =lang('500_email_existed','tip');
						} else {
							$message =lang('500_unknown_faild','tip');
						}
						$this->ajax_failed_response($message);
						return FALSE;
					}

					$data['uc_id'] = $uc_id;
					$data['uc_nickname'] = $uc_username;
				}
					
				$user_id = $userdb->add_one($data);
				load_helper('view');
				create_default_avatar($user_id);
				$user = $userdb->getuser_byid($user_id);
				$user['password'] = $password;

				load_api('event')->register($user_id);

				load_api('user')->setsession($user);
				$this->ajax_success_response(array('url'=>$url), lang('200_registersuccess','tip'));
				return TRUE;
			}
		}
		$this->socialapis = load_api('setting')->getSetting('socialapi');
		$this->output("user/register");
	}

	public function validemail(){
		$userapi = load_api('user');
		$userapi->removesession();
		if($hash = $this->param('hash')){
			list($uid,$createtime)=explode('-',pzxdecode($hash));
			$usergroups = $userapi->getGroupsByUser($uid);
			if($usergroups['system']==5){
				$user = $userapi->getUserByid($uid);
				if($user['create_time']==$createtime){
					$userapi->updateUserSystemGroupMember($uid);
					$this->redirect(site_url(config_item('default_module')), lang('200_validemailsuccess','tip'));
					return;
				}
			}
		}

		$this->redirect(site_url(config_item('default_module')), lang('500_validemailfaild','tip'));
	}

	public function bbslogin(){
		$url = ($_SERVER['HTTP_REFERER']==null||$_SERVER['HTTP_REFERER']==site_url('user/login'))?site_url(config_item('default_module')):$_SERVER['HTTP_REFERER'];
		if($this->islogin){
			$this->redirect($url, lang('200_loginsuccess','tip'));
			return;
		}
		$userapi = load_api('user');
		if($data['bbs_username'] = $this->param("bbs_username")){
			$data['bbs_password'] = $this->param('bbs_password');
			$result = $userapi->bbs_login($data);
			if($result===501){
				$this->ajax_failed_response(lang('501_password_wrong','tip'));
			}elseif($result===502){
				$this->ajax_failed_response(lang('502_user_notexist','tip'));
			}else{
				$this->ajax_success_response(array('url'=>$url), lang('200_loginsuccess','tip').$result);
			}
			return;
		}
		return;
	}

	public function profile(){
		$uid =  $this->param("dataid");
		if($uid&&is_numeric($uid)){
			$this->user_id = $uid;
		}
		if($this->user_id){
			$user = load_api('user')->getUserWithGroupByid($uid);
			$user['passwd'] = '';
			$user['avatar'] = useravatar($user['user_id'], 'middle');
			$user['home'] = site_url('home/user?uid='.$this->user_id);
			$shares = load_api('share')->search(array('user_id'=>$this->user_id,'type'=>'noarticle'),1,12);
			$shares = load_api('share')->prepareShares($shares);
			$relation = $this->relationView($this->cuser['uid'], $this->user_id);
			$setting = load_api('setting')->getSetting('credit');
			$user['credits'] = array('name'=>lang('credits','view'),'value'=>$user['credits']);
			$user['credits1'] = array('name'=>$setting['ext_credits_1'],'value'=>$user['ext_credits_1']);
			$user['credits2'] = array('name'=>$setting['ext_credits_2'],'value'=>$user['ext_credits_2']);
			$user['credits3'] = array('name'=>$setting['ext_credits_3'],'value'=>$user['ext_credits_3']);
			$this->ajax_success_response(array('user'=>$user,'shares'=>$shares,'relation'=>$relation),'');
			return;
		}
		$this->ajax_failed_response();
	}

	public function updateuserinfo(){
		if($this->param('nickname')){
			$data['nickname'] = $this->param('nickname');
			$data['gender'] = $this->param('gender','none');
			$data['province'] = $this->param('province');
			$data['user_title'] = $this->param('usertitle','');
			$data['city'] = $this->param('city','');
			$data['location'] = $this->param('location','');
			$data['domain'] = $this->param('domain','');
			$data['bio'] = $this->param('bio','');
			$userdb = load_db('user');
			if ($data['nickname']!=$this->cuser['nickname']&&!$userdb->checknick($data['nickname'])) {
				$this->ajax_failed_response(sprintf(lang('cnicknamecheck','form_validation'),$data['nickname']));
				return;
			}
			if($data['domain']){
				$user = $userdb->find(array('domain'=>$data['domain']));
				if ($user&&($user['user_id']!=$this->cuser['uid'])) {
					$this->ajax_failed_response(sprintf(lang('alreadyexist','form_validation'),$data['domain']));
					return;
				}
			}
			$fvalidation = load_class('Form_validation');

			$fvalidation->set_rules('nickname', 'lang:nickname:field' , 'trim|required|min_length[2]|max_length[20]|xss_clean');

			if ($fvalidation->run() == FALSE){
				$this->ajax_failed_response($fvalidation->error_string());
				return FALSE;
			}else{
				if($userdb->update(array('user_id'=>$this->cuser['uid']),$data)){
					load_api('user')->forceRefreshLoginInfo();
					$this->ajax_success_response(null, lang('200_operatesuccess','tip'));
				}else{
					$this->ajax_failed_response(lang('500_operatefaild','tip'));
				}
			}
		}

	}

	public function nicknameupdatecheck(){
		$nickname = $this->param('nickname');
		if ($nickname!=$this->cuser['nickname']&&!load_db('user')->checknick($nickname)) {
			$this->ajax_echo('false');
		}else{
			$this->ajax_echo('true');
		}
	}

	public function domaincheck(){
		$domain = $this->param('domain');
		$user = load_db('user')->find(array('domain'=>$domain));
		if (!$user||($user['user_id']==$this->cuser['uid'])) {
			$this->ajax_echo('true');
		}else{
			$this->ajax_echo('false');
		}
	}

	public function resetpasswd(){
		if($this->param('new_passwd')){
			$ptx_user = load_db('user');
			if($this->cuser['is_social']){
				$data['email'] = $this->param('email');
				$new_passwd = $this->param('new_passwd');
				$data['password'] = $new_passwd;
				$data['is_social'] = 0;
					
				$response = $ptx_user->reset_passwd($this->cuser['uid'],$data,$new_passwd,TRUE);
				$this->ajax_response($response);
				return;
			}else {
				$fvalidation = load_class('Form_validation');
				$fvalidation->set_rules('new_passwd', 'lang:password:field' , 'trim|required|min_length[6]|max_length[40]');
				if ($fvalidation->run() == FALSE){
					$this->ajax_failed_response($fvalidation->error_string());
					return FALSE;
				}

				$org_passwd = $this->param('org_passwd');
				$new_passwd = $this->param('new_passwd');
				if($org_passwd==$new_passwd){
					$this->ajax_failed_response(lang('500_password_can_not_same','tip'));
					return;
				}
				$user = $ptx_user->getuser_byid($this->cuser['uid']);
				if($user['passwd'] != md5($org_passwd)){
					$this->ajax_failed_response(lang('501_password_wrong','tip'));
					return;
				}

				$values['passwd']=md5($new_passwd);
				load_db('user')->update(array('user_id'=>$this->cuser['uid']), $values);
				$ucenter = spClass("Ucenter");
				$ucopen = config_item('UC_OPEN','uc');
				if($ucopen){
					$user = $this->getuser_byid($this->cuser['uid']);
					if($user['uc_id']){
						$ucenter = load_api('ucenter');
						$ucenter->uc_user_edit($user['nickname'],null, $new_passwd, null,1);
					}
				}
				$this->ajax_success_response(null);
				return;
			}
		}
	}

	public function cemailcheck($postdata, $param){
		$userdb = load_db('user');
		return $userdb->checkemail($postdata);
	}


	public function cnicknamecheck($postdata, $param){
		$userdb = load_db('user');
		return $userdb->checknick($postdata);
	}

	public function emailcheck(){
		$userdb = load_db('user');
		$email = $this->param("email");
		if($this->islogin&&$this->cuser['email']==$email){
			$this->ajax_echo('true');
			return;
		}
		if($userdb->checkemail($email)){
			$this->ajax_echo('true');
		}else{
			$this->ajax_echo('false');
		}
	}

	public function nicknamecheck(){
		$userdb = load_db('user');
		$nickname = $this->param("nickname");
		if($userdb->checknick($nickname)){
			$this->ajax_echo('true');
		}else{
			$this->ajax_echo('false');
		}
	}


	public function uploadavatar(){
		$attachmentapi = load_api('attachment');
		$settings =  $this->settings;
		$allowedExtensions = array('jpg','jpeg','gif','png');
		$sizeLimit = 2 * 1024 * 1024;
		$temp_dir = BASEPATH.'data/attachments/tmp/';
		(!is_dir($temp_dir))&&@mkdir($temp_dir,0777,true);
		$result = $attachmentapi->simpleUpload($temp_dir,false,$allowedExtensions,$sizeLimit);
		$this->ajax_response($result);
		return;
	}

	public function saveavatar(){
		$x = $this->param("x");
		$y = $this->param("y");
		$w = $this->param("w");
		$h = $this->param("h");
		$js_w = $this->param("js_w");
		$js_h = $this->param("js_h");
		$type = $this->param("type");
		$filename = $this->param("filename");
		$imagepath = BASEPATH.'data/attachments/tmp/'.$filename;

		if($filename&&filename_check($filename)&&file_exists($imagepath)){
			$imagelib = &load_class('ImageLib');
			$image_size = @getimagesize($imagepath);
			$weight=$image_size[0];
			$height=$image_size[1];
			if($js_w<$weight){
				$scale = $js_w/$weight;
			}elseif ($js_h<$height){
				$scale = $js_h/$height;
			}else{
				$scale = 1;
			}
			$x = $x/$scale;
			$y = $y/$scale;
			$w = $w/$scale;
			$h = $h/$scale;

			$imagelib->crop_image($imagepath,$imagepath,$x,$y,$w,$h);

			$ptx_user = load_db('user');
			$avatar_dir = BASEPATH.useravatardir($this->cuser['uid']);
			(!is_dir($avatar_dir))&&@mkdir($avatar_dir,0777,true);
			if($type=='avatar'){
				if($name = substr($this->cuser['uid'], -2)){
					$filename = $name.'_avatar';
				}else{
					$filename = $this->cuser['uid'].'_avatar';
				}
				//$filename = substr($this->cuser['uid'], -2).'_avatar';
				$orgin = $avatar_dir.$filename.".jpg";
				$large = $avatar_dir.$filename."_large.jpg";
				$middle = $avatar_dir.$filename."_middle.jpg";
				$small = $avatar_dir.$filename."_small.jpg";
				file_exists($orgin) && unlink($orgin);
				file_exists($large) && unlink($large);
				file_exists($middle) && unlink($middle);
				file_exists($small) && unlink($small);

				@copy($imagepath, $orgin);
				$imagelib->create_thumb($imagepath,NULL,150,150,$large);
				$imagelib->create_thumb($imagepath,NULL,50,50,$middle);
				$imagelib->create_thumb($imagepath,NULL,16,16,$small);
				unlink($imagepath);

				load_api('event')->setavatar($this->cuser['uid']);
				$data['large']= useravatar($this->cuser['uid'],'large');
				$data['middle']= useravatar($this->cuser['uid'],'middle');
				$data['small']= useravatar($this->cuser['uid'],'small');
			}else if($type=='banner'){
				file_exists($avatar_dir.$avatar_info['banner']) && unlink($avatar_dir.$avatar_info['banner']);
				$imagelib->create_thumb($imagepath,NULL,950,300,$avatar_dir.$avatar_info['banner']);
				unlink($imagepath);
			}else if($type=='topic'){
				$imagelib->create_thumb($imagepath,NULL,0,400,$imagepath);
				$data['banner'] = 'data/attachments/tmp/'.$filename;
			}else if($type=='group'){
				$imagelib->create_thumb($imagepath,NULL,0,400,$imagepath);
				$data['banner'] = 'data/attachments/tmp/'.$filename;
			}else if($type=='store'){
				$imagelib->create_thumb($imagepath,NULL,0,300,$imagepath);
				$data['banner'] = 'data/attachments/tmp/'.$filename;
			}

			$data['hash'] = uniqid();
			$this->ajax_success_response($data, '');
			return;
		}
		$this->ajax_failed_response(lang('500_operatefaild','tip'));
		return;
	}

	public function forgetpassword(){
		if($to_email=$this->param("email")){
			if($user = load_api('user')->getUserByEmail($to_email)){
				$password_key = random_string('unique');
				$password_expire = time() + 3600*48;
				if(load_db('user')->update_password_key($to_email,$password_key,$password_expire)) {
					$emailapi = load_api('email');
					$emailapi->asyn_sendmail('findpassword',$user['user_id']);
					$this->ajax_success_response(array('url'=>site_url(config_item('default_module'))),lang('200_submitsuccess','tip'));
					return;
				}
			}
			$this->ajax_failed_response(lang('500_operatefaild','tip'));
		} else {
			$this->output("user/forgetpassword");
		}
	}

	public function resetpassword(){
		if($passkey=$this->param("passkey")){
			$ptx_user = load_db('user');
			$user = $ptx_user->find(array('lost_password_key'=>$passkey));
			$this->expired = ($user['lost_password_expire'] > time())?false:true;
			$this->passkey = $passkey;
			if($new_passwd=$this->param("new_passwd")){
				if(!$this->expired){
					if(load_api('user')->resetpasswd($user['user_id'],$new_passwd)){
						load_db('user')->update_password_key($user['email'],'','');
						$this->ajax_success_response(array('url'=>site_url(config_item('default_module'))),lang('200_operatesuccess','tip'));
						return;
					}
				}
				$this->ajax_failed_response(lang('500_operatefaild','tip'));
			}
			$this->output("user/resetpassword");
		}else{
			$this->jump(site_url(config_item('default_module')));
		}
	}

	public function facewall(){
		$settingapi = load_api('setting');
		$setting = $settingapi->getSetting('layoutalbum');
		$num_per_page = $setting['uialbum_numperpage']?$setting['uialbum_numperpage']:20;
		$numbershowpage = $setting['uialbum_numbershowpage']?$setting['uialbum_numbershowpage']:0;

		$page = $this->param('page','1');
		$wpage = $this->param('wpage');
		$args = array("page"=>"2","wf"=>"1");
		if($wpage&&$numbershowpage){
			$data['page'] = ($wpage-1)*$numbershowpage+$page;
			$args['wpage'] = $data['wpage'] = $wpage;
		}else{
			$data['page'] = $page;
		}
		$data['showpage'] = (($numbershowpage)&&($data['page']%$numbershowpage==0))?'1':'0';
		
		$order =  $this->param("sort",'fans');
		$sort = null;
		if(in_array($order, array('fans','reg','share','like','nickname'))){
			if($order=='fans'){
				$sort = ' ptx_user.total_follower DESC ';
			}else if($order=='reg'){
				$sort = ' ptx_user.create_time DESC ';
			}else if($order=='share'){
				$sort = ' ptx_user.total_share DESC ';
			}else if($order=='like'){
				$sort = ' ptx_user.total_like DESC ';
			}else if($order=='nickname'){
				$sort = ' ptx_user.nickname ASC ';
			}
			$args['sort'] = $order;
			$data['sort'] = $order;
		}

		$userdb = load_db('user');
		$users = $userdb->searchWithShare(null,$data['page'],$num_per_page,null,$sort);
		$pager = &load_class('Pager', 'core');
		$data['pages'] = $pager->createPages('user/facewall',$args);
		$data['nextpage_url'] = site_url_param('user/facewall',$args);
		unset($args['wf']);
		unset($args['page']);
		$data['wpages'] = $pager->createCustomPages('user/facewall',$args,'wpage',$num_per_page*$numbershowpage);
		$user_id = $this->islogin?$this->cuser['uid']:0;
		$users = $this->addrelation($users,$user_id);
		$data['tpl_waterfall'] = $this->render('waterfall/user',array('users'=>$users,'showpage'=>$data['showpage']));
		$data['layoutsetting'] = $setting;
		$this->output("user/facewall",$data,($this->param('wf'))?false:true);
	}


	private function addrelation($friends,$user_id){
		foreach ($friends as $key=>$friend){
			$friends[$key]['relation_view'] = $this->relationView($user_id,$friend['user_id']);
		}
		return $friends;
	}


	public function addfollow(){
		$friend_id = $this->param("fid");
		if(!is_numeric($friend_id)){
			$this->ajax_failed_response(lang('illegal_operation','tip'));
			return;
		}
		$userdb=load_db('user');
		$user = $userdb->find(array('user_id'=>$this->cuser['uid']),' ptx_user.total_follow ');
		if($user['total_follow']>=$this->memperm['maxfollow']&&!$this->isadmin&&!$this->iseditor){
			$this->ajax_failed_response(lang('reach_max_follow_num','tip').$this->memperm['maxfollow']);
			return;
		}

		$friend = $userdb->find(array('user_id'=>$friend_id));
		if(!$friend){
			$this->ajax_failed_response(lang('500_operatefaild','tip'));
			return;
		}
		$ptx_relationship = load_db('user/userrel');
		$result = $ptx_relationship->add_follow($this->cuser['uid'],$friend_id);
		if($result){
			$data = $this->relationView($this->cuser['uid'], $friend_id);
			$evtapi = load_api('event');
			$evtapi->follow($this->cuser['uid'],'',0,$share['user_id']);
			$evtapi->beenfollow($friend_id);
			$this->ajax_success_response($data, '');
			return;
		}else{
			$this->ajax_failed_response(lang('500_operatefaild','tip'));
			return;
		}
	}

	public function removefollow(){
		$friend_id = $this->param("fid");
		if(!is_numeric($friend_id)){
			$this->ajax_failed_response(lang('illegal_operation','tip'));
			return;
		}
		$ptx_relationship = load_db('user/userrel');
		$result = $ptx_relationship->remove_follow($this->cuser['uid'],$friend_id);
		if($result){
			$data = $this->relationView($this->cuser['uid'], $friend_id);
			$this->ajax_success_response($data, '');
			return;
		}else{
			$this->ajax_failed_response(lang('500_operatefaild','tip'));
			return;
		}
	}

	public function savetheme(){
		$bgrepeat = $this->param("bgrepeat");
		$bgpos = $this->param("bgpos");
		$bglock = $this->param("bglock");
		$headerrepeat = $this->param("headerrepeat");
		$headerpos = $this->param("headerpos");
		$headerlock = $this->param("headerlock");
		$filename = $this->param("filename");
		$headerfilename = $this->param("headerfilename");
		$usertheme = $this->param("usertheme");
		$themetype = $this->param("themetype");
		$storeid = $this->param("storeid");
		$headerimagepath = BASEPATH.'data/attachments/tmp/'.$headerfilename;
		$imagepath = BASEPATH.'data/attachments/tmp/'.$filename;

		if($themetype=='user'){
			$udata = ucache('r', 'css', $this->cuser['uid']);
			if(!$udata){
				$udata['headerbgpath'] = userrespath($this->cuser['uid'],'headerbg','jpg');
				$udata['bgpath'] = userrespath($this->cuser['uid'],'bg','jpg');
				$udata['bgcss'] = userrespath($this->cuser['uid'],'bg','css');
				$udata['bgpos'] = 'center center';
				$udata['bglock'] = 'scroll';
				$udata['bgrepeat'] = 'repeat';
				$udata['headerpos'] = 'center center';
				$udata['headerlock'] = 'scroll';
				$udata['headerrepeat'] = 'repeat';
			}
			$udata['customheader'] = $this->param("customheader");
			$udata['custombg'] = $this->param("custombg");

			$udata['bgpos'] = $bgpos?$bgpos.' center':'center center';
			$udata['bglock'] = $bglock?'fixed':'scroll';
			$udata['bgrepeat'] = $bgrepeat?'repeat':'no-repeat';

			$udata['headerpos'] = $headerpos?$headerpos.' center':'center center';
			$udata['headerlock'] = $headerlock?'fixed':'scroll';
			$udata['headerrepeat'] = $headerrepeat?'repeat':'no-repeat';
			
			$headerorgin = BASEPATH.$udata['headerbgpath'];
			$bgorgin = BASEPATH.$udata['bgpath'];
			$bgcss = BASEPATH.$udata['bgcss'];

			if($headerfilename&&filename_check($headerfilename)&&file_exists($headerimagepath)){
				file_exists($headerorgin) && unlink($headerorgin);
				@copy($headerimagepath, $headerorgin);
				unlink($headerimagepath);
			}
				
			if($filename&&filename_check($filename)&&file_exists($imagepath)){
				file_exists($bgorgin) && unlink($bgorgin);
				@copy($imagepath, $bgorgin);
				unlink($imagepath);
				$udata['theme'] = false;
			}
			if($usertheme){
				load_db('user')->update(array('user_id'=>$this->cuser['uid']),array('theme'=>$usertheme));
				$udata['theme'] = $usertheme;
			}
			ucache('w', 'css', $this->cuser['uid'],$udata);
			$cssstr = $this->render('user/csstpl.css',$udata);
			@file_put_contents($bgcss, $cssstr);
				
			$this->ajax_success_response(null,lang('200_operatesuccess','tip'));
			return;
		}else{
			if($usertheme){
				load_db('store')->update(array('store_id'=>$storeid),array('theme'=>$usertheme));
				if(!$filename){
					$storeres = load_api('store')->storeres($storeid);
					$csspath = BASEPATH.$storeres['dir'].$storeres['css'];
					file_exists($csspath) && unlink($csspath);
				}
			}else{
				load_db('store')->update(array('store_id'=>$storeid),array('theme'=>''));
				$storeres = load_api('store')->storeres($storeid);
				$csspath = BASEPATH.$storeres['dir'].$storeres['css'];
				file_exists($csspath) && unlink($csspath);
			}
				
			$this->ajax_success_response(null,lang('200_operatesuccess','tip'));
			return;
		}
		
	}

	public function reset_password_succeed(){
		$this->output("user/reset_password_succeed");
	}
	public function forget_password_succeed(){
		$this->output("user/forget_password_succeed");
	}
}